package com.nbsaas.boot;

import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/persons")
public class PersonController {

    private final List<Person> persons = new ArrayList<>();

    @GetMapping
    public Flux<Person> getAllPersons() {
        return Flux.fromIterable(persons);
    }

    @GetMapping("/{id}")
    public Mono<Person> getPersonById(@PathVariable String id) {
        return Mono.justOrEmpty(persons.stream()
                .filter(person -> person.getId().equals(id))
                .findFirst());
    }

    @PostMapping
    public Mono<Void> addPerson(@RequestBody Person person) {
        persons.add(person);
        return Mono.empty();
    }
}
